package logic

import (
	"context"
	"fmt"
	"novel/model/mysql"
	"strconv"

	"novel/novelrpc/internal/svc"
	"novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type BookAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBookAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BookAddLogic {
	return &BookAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 创建小说
func (l *BookAddLogic) BookAdd(in *novelrpc.BookAddRequest) (*novelrpc.BookAddResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.DB.Where("id=?", in.UserId).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("该用户不存在")
	}
	if in.Text == "" || in.Types == "" || in.Title == "" {
		return nil, fmt.Errorf("标题、分类、简介必填字段")
	}

	data := mysql.Book{
		UserId: in.UserId,
		Title:  in.Title,
		Types:  in.Types,
		Label:  in.Label,
		Text:   in.Text,
	}
	mysql.DB.Create(&data)
	return &novelrpc.BookAddResponse{
		BookId: strconv.Itoa(int(data.ID)),
	}, nil
}
